﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using DTO;
using BUS;
using PopupControl;
using ServiceSearch;

namespace simpleGUI_QLQTPM
{
    public partial class Form_TimKiem_Tu : Form
    {
        #region Controls
        private Popup gridToolTip = null;
        private GridToolTip gridCustomToolTip = null;
        private Popup viewPopup = null;
        private ViewProviders viewProviders = null;
        #endregion

        #region Attributes
        private static EventWaitHandle _waitHandle = new AutoResetEvent(false);
        private static int ROWS_PER_PAGE = 20;

        public Form _parent = null;
        private bool _arrowDown = true;
        private string _strDichVu = String.Empty;
        private int _widthRate = 0;
        private int _heightRate = 0;
        private int _leftRate = 0;
        private int _topRate = 0;

        private SearchEngine _se = null;
        private Thread _searchThread = null;
        private AutoComplete _ac = null;
        private List<DuLieuDTO> _lstDuLieuDTO = null;
        private int _pageIdx = 0;
        private bool _firstLoadAdvanceSearch = true;
        private string _oldKeyword = String.Empty;
        private bool _searchAvailable = true;
        #endregion

        public Form_TimKiem_Tu(string strDichVu)
        {
            InitializeComponent();
            this._strDichVu = strDichVu;

            this.viewPopup = new Popup(this.viewProviders = new ViewProviders());
            this.viewPopup.AutoClose = false;
            this.viewPopup.ShowingAnimation = PopupAnimations.Slide | PopupAnimations.TopToBottom;
            this.viewPopup.HidingAnimation = PopupAnimations.Slide | PopupAnimations.BottomToTop;
            this.viewProviders.ProviderChanged += fireView_Event;

            this.gridToolTip = new Popup(this.gridCustomToolTip = new GridToolTip());
            this.gridToolTip.AutoClose = false;
            this.gridToolTip.FocusOnOpen = false;
            this.gridToolTip.ShowingAnimation = this.gridToolTip.HidingAnimation = PopupAnimations.Blend;
        }

        #region Interface
        private void LB_CapNhatDuLieu_MouseEnter(object sender, EventArgs e)
        {
            Panel_CapNhatDuLieu_MouseEnter(sender, e);
        }

        private void LB_CapNhatDuLieu_MouseLeave(object sender, EventArgs e)
        {
            Panel_CapNhatDuLieu_MouseLeave(sender, e);
        }

        private void Panel_CapNhatDuLieu_MouseEnter(object sender, EventArgs e)
        {
            Panel_CapNhatDuLieu.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.staticbutton2_hover;
        }

        private void Panel_CapNhatDuLieu_MouseLeave(object sender, EventArgs e)
        {
            Panel_CapNhatDuLieu.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.staticbutton2;
        }

        private void PB_CapNhatDuLieu_MouseEnter(object sender, EventArgs e)
        {
            Panel_CapNhatDuLieu_MouseEnter(sender, e);
        }

        private void PB_CapNhatDuLieu_MouseLeave(object sender, EventArgs e)
        {
            Panel_CapNhatDuLieu_MouseLeave(sender, e);
        }

        private void Panel_TimKiem_MouseEnter(object sender, EventArgs e)
        {
            //Panel_TimKiem.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.staticbutton4_hover;
        }

        private void Panel_TimKiem_MouseLeave(object sender, EventArgs e)
        {
           // Panel_TimKiem.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.staticbutton4;
        }

        private void PB_TimKiem_MouseEnter(object sender, EventArgs e)
        {
            Panel_TimKiem_MouseEnter(sender, e);
        }

        private void PB_TimKiem_MouseLeave(object sender, EventArgs e)
        {
            Panel_TimKiem_MouseLeave(sender, e);
        }

        private void Button_Tim_MouseEnter(object sender, EventArgs e)
        {
            Panel_Tim.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.staticbutton6_hover;
        }

        private void Button_Tim_MouseLeave(object sender, EventArgs e)
        {
            Panel_Tim.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.staticbutton6;
        }

        private void Button_QuayLai_MouseEnter(object sender, EventArgs e)
        {
            //Panel_QuayLai.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.staticbutton6_hover;
        }

        private void Button_QuayLai_MouseLeave(object sender, EventArgs e)
        {
            //Panel_QuayLai.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.staticbutton6;
        }

        private void Button_GoogleMap_MouseEnter(object sender, EventArgs e)
        {
           // Panel_GoogleMap.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.staticbutton6_hover;
        }

        private void Button_GoogleMap_MouseLeave(object sender, EventArgs e)
        {
           // Panel_GoogleMap.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.staticbutton6;
        }

        void Size_Rate(int width, int height, int left, int top)
        {
            _widthRate = (width * 10000) / 810;
            _heightRate = (height * 10000) / 600;
            _leftRate = (left * 10000) / 810;
            _topRate = (top * 10000) / 600;
        }

        void Resize_Control(Control TempControl)
        {
            TempControl.Width = (this.Width * _widthRate) / 10000;
            TempControl.Height = (this.Height * _heightRate) / 10000;
            TempControl.Left = (this.Width * _leftRate) / 10000;
            TempControl.Top = (this.Height * _topRate) / 10000;
        }

        private void Form_TimKiem_SizeChanged(object sender, EventArgs e)
        {
            Size_Rate(479, 74, 292, 33); Resize_Control(PB_Noti_Text);
            Size_Rate(48, 48, 239, 47); Resize_Control(PB_Noti_Icon);
          //  Size_Rate(183, 35, 0, 125); Resize_Control(Panel_TimKiem);
            Size_Rate(186, 96, 0, 167); Resize_Control(Panel_CapNhatDuLieu);
            Size_Rate(600, 34, 200, 126); Resize_Control(PB_Title);
            Size_Rate(449, 36, 205, 166); Resize_Control(PB_Input);
            Size_Rate(433, 22, 212, 174); Resize_Control(TB_Input);
            Size_Rate(134, 35, 660, 166); Resize_Control(Panel_Tim);
            Size_Rate(449, 34, 205, 206); Resize_Control(PB_Combobox);
            Size_Rate(433, 23, 212, 211); Resize_Control(CB_DichVu);
            //Size_Rate(107, 13, 209, 243); Resize_Control(LB_HTKQ);
            Size_Rate(112, 17, 209, 258); Resize_Control(LB_HuongTK);
           // Size_Rate(119, 14, 638, 252); Resize_Control(LB_HinhThucHienThi);
           // Size_Rate(31, 24, 751, 248); Resize_Control(PB_Arrow);
           // Size_Rate(582, 219, 212, 286); Resize_Control(DGV_KetQua);
            Size_Rate(177, 30, 9, 465); Resize_Control(LB_Status);
          //  Size_Rate(145, 35, 204, 520); Resize_Control(Panel_GoogleMap);
//Size_Rate(145, 35, 356, 520); Resize_Control(Panel_QuayLai);
            Size_Rate(598, 233, 205, 280); Resize_Control(PB_Datagrid);
            Size_Rate(582, 219, 212, 286); Resize_Control(ffpResult);
            //Size_Rate(600, 126, 205, 116); Resize_Control(panel_KhungTimNangCao);
        }
        #endregion

        private void Form_TimKiem_Load(object sender, EventArgs e)
        {
            // khoi tao Search Engine
            bool success = true;
            try
            {
                this._se = new SearchEngine();
                this._ac = new AutoComplete();
            }
            catch (System.Exception ex)
            {
                success = false;
                MessageBox.Show("Initialize failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (!success)
            {
                try
                {
                    this.Close();
                }
                catch (System.Exception ex)
                {

                }
            }

            // nap du lieu vao combo box DichVu
            List<DichVuDTO> lstDichVuDTO = this._se.GetAllDichVuDTO();
            lstDichVuDTO.Sort(
                delegate(DichVuDTO dv1, DichVuDTO dv2)
                {
                    return dv1.TenDichVu.CompareTo(dv2.TenDichVu);
                }
            );
            DichVuDTO dichVuDTO = new DichVuDTO();
            dichVuDTO.MaDichVu = 0;
            dichVuDTO.TenDichVu = "Tất cả dịch vụ";
            lstDichVuDTO.Insert(0, dichVuDTO);

            this.CB_DichVu.DataSource = lstDichVuDTO;
            this.CB_DichVu.ValueMember = "MaDichVu";
            this.CB_DichVu.DisplayMember = "TenDichVu";

            int idx = 0;
            for (idx = 0; idx < lstDichVuDTO.Count; idx++)
            {
                if (this._strDichVu == lstDichVuDTO[idx].TenDichVu)
                    break;
            }

            if (idx >= lstDichVuDTO.Count)
            {
                idx = 0;
            }

            this.CB_DichVu.SelectedIndex = idx;

            // them tooltip vao cac control
            ToolTip tt = new ToolTip();
            //tt.SetToolTip(this.PB_Tim, "Kích hoạt tìm kiếm với từ khóa vừa nhập");
            tt.SetToolTip(this.TB_Input, "Nơi nhập từ khóa tìm kiếm. Ví dụ: ATM Nguyen Van Cu");
            tt.SetToolTip(this.CB_DichVu, "Chọn loại hình dịch vụ cần tìm");
            //tt.SetToolTip(this.cbb_DichVu, "Chọn loại hình dịch vụ");
            //tt.SetToolTip(this.cbb_Duong, "Chọn đường cần tìm");
            //tt.SetToolTip(this.cbb_Phuong, "Chọn phường cần tìm");
            //tt.SetToolTip(this.cbb_Quan, "Chọn quận cần tìm");

            // an paging control
            this.LB_Truoc.Visible = false;
            this.LB_Sau.Visible = false;
			this.xuatKQNgay();
        }

		private void xuatKQNgay()
        {
            try
            {
                if (null != this._searchThread)
                {
                    if (this._searchThread.ThreadState == ThreadState.Running)
                    {
                        try
                        {
                            this._searchThread.Abort();
                        }
                        catch (System.Exception ex)
                        {

                        }

                        Thread.Sleep(100);
                    }
                }

                string strInput = "";
                int maDichVu = (int)this.CB_DichVu.SelectedValue;
                SearchInputObject si = new SearchInputObject(strInput, maDichVu);
                this._searchThread = new Thread(new ParameterizedThreadStart(this.SearchThread));
                this._searchThread.Start(si);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
		
        private void Form_TimKiem_FormClosing(object sender, FormClosingEventArgs e)
        {
            //this._parent.Dispose();
        }

        private void Panel_TimKiem_Click(object sender, EventArgs e)
        {
            this._parent.Location = this.Location;
            this._parent.Visible = true;
            this.Dispose();
        }

        private void PB_QuayLai_Click(object sender, EventArgs e)
        {
            Panel_TimKiem_Click(sender, e);
        }

        private void ShowPopup(Panel popup)
        {
            int step = 3;
            Size old = popup.Size;
            popup.Size = new Size(old.Width, 0);

            while (popup.Size.Height < old.Height)
            {
                popup.Size = new Size(old.Width, popup.Size.Height + step);
                popup.Refresh();
            }

            popup.Size = old;
        }

        private void HidePopup(Panel popup)
        {
            int step = 3;
            Size old = popup.Size;

            while (popup.Size.Height > 0)
            {
                popup.Size = new Size(old.Width, popup.Size.Height - step);
                popup.Refresh();
            }

            popup.Size = old;
        }

        private void AddItems(Panel popup, List<string> items)
        {
            Font myFont = new Font("Calibri", 9, FontStyle.Bold);
            String plaintext = "";
            List<Label> myLabels = new List<Label>();
            Point hook = new Point(popup.Location.X + popup.Width, popup.Location.Y);
            Point curLocation = new Point();

            for (int i = 0; i < items.Count; i++)
            {
                plaintext += items[i] + "\n\n";

                Label myLabel = new Label();

                myLabel.Text = items[i];
                myLabel.Visible = true;
                myLabel.Font = myFont;

                myLabels.Add(myLabel);
            }

            popup.Size = TextRenderer.MeasureText(plaintext, myFont);
            popup.Location = new Point(hook.X - popup.Width, hook.Y);

            foreach (Label myLabel in myLabels)
            {
                myLabel.Location = curLocation;
                popup.Controls.Add(myLabel);

                curLocation = new Point(curLocation.X + popup.Size.Height / myLabels.Count, curLocation.Y);
            }
        }

        private void PB_Arrow_Click(object sender, EventArgs e)
        {
            if (_arrowDown)
            {
                //PB_Arrow.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.arrow_up;
                _arrowDown = false;

                //this.viewPopup.Show(this.PB_Arrow, new Point(-120, 22));
            }
            else
            {
                //PB_Arrow.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.arrow_down;
                _arrowDown = true;

                this.viewPopup.Close();
            }
        }

        private void PB_Xoa_Click(object sender, EventArgs e)
        {
            /*try
            {
                listBox2.Visible = false;
                if (null != this._searchThread)
                {
                    if (this._searchThread.ThreadState == ThreadState.Running)
                    {
                        try
                        {
                            this._searchThread.Abort();
                        }
                        catch (System.Exception ex)
                        {

                        }

                        Thread.Sleep(100);
                    }
                }

                string strInput = this.TB_Input.Text;
                int maDichVu = (int)this.CB_DichVu.SelectedValue;
                if (0 == strInput.Trim().Length && 0 == maDichVu)
                {
                    MessageBox.Show("Vui lòng nhập từ khóa để thực hiện tìm kiếm.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SearchInputObject si = new SearchInputObject(strInput, maDichVu);
                    this._searchThread = new Thread(new ParameterizedThreadStart(this.SearchThread));
                    this._searchThread.Start(si);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            //*/TB_Input.Text = "";
        }
        private void PB_Tim_Click(object sender, EventArgs e)
        {
            try
            {
                listBox2.Visible = false;
                if (null != this._searchThread)
                {
                    if (this._searchThread.ThreadState == ThreadState.Running)
                    {
                        try
                        {
                            this._searchThread.Abort();
                        }
                        catch (System.Exception ex)
                        {

                        }

                        Thread.Sleep(100);
                    }
                }

                string strInput = this.TB_Input.Text;
                int maDichVu = (int)this.CB_DichVu.SelectedValue;
                if (0 == strInput.Trim().Length && 0 == maDichVu)
                {
                    MessageBox.Show("Vui lòng nhập từ khóa để thực hiện tìm kiếm.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SearchInputObject si = new SearchInputObject(strInput, maDichVu);
                    this._searchThread = new Thread(new ParameterizedThreadStart(this.SearchThread));
                    this._searchThread.Start(si);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           
        }

        private void SearchThread(object objInput)
        {
            try
            {
                Thread searchThread = new Thread(new ParameterizedThreadStart(this.SearchingThread));
                searchThread.Start(objInput);
                Form_ThongBao frm = new Form_ThongBao();
                this.Invoke((MethodInvoker)delegate
                {
                    frm.Show();
                });

                _waitHandle.WaitOne();
                this.Invoke((MethodInvoker)delegate
                {
                    frm.Close();
                });
            }
            catch (System.Exception ex)
            {
                Utilities.WriteLog(ex.Message);
            }
        }

        private void SearchingThread(object objInput)
        {
            try
            {
                SearchInputObject si = (SearchInputObject)objInput;
                string strInput = si.InputString;
                int maDichVu = si.MaDichVu;

                this._pageIdx = 0;
                this.Invoke((MethodInvoker)delegate
                {
                    //this.DGV_KetQua.Rows.Clear();
                    this.ffpResult.Controls.Clear();
                });

                this._lstDuLieuDTO = this._se.Search(strInput, maDichVu);
                if (this._lstDuLieuDTO.Count > 0)
                {
                    ShowPage(this._pageIdx);

                    this.Invoke((MethodInvoker)delegate
                    {
                        this.LB_HuongTK.Text = "Có " + this._lstDuLieuDTO.Count.ToString() + " địa điểm thỏa yêu cầu";
                    });
                }
                else
                {
                    this.Invoke((MethodInvoker)delegate
                    {
                        this.LB_HuongTK.Text = "Hiện chúng tôi chưa có thông tin bạn muốn tìm";
                    });
                }
            }
            catch (System.Exception ex)
            {
                Utilities.WriteLog(ex.Message);
            }
            finally
            {
                _waitHandle.Set();
            }
        }

        public void PrepareListResult(List<DuLieuThoDTO> lstDuLieuThoDTO)
        {
            if (null != lstDuLieuThoDTO)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    //this.DGV_KetQua.Rows.Clear();
                });

                for (int i = 0; i < lstDuLieuThoDTO.Count; i++)
                {
                    DuLieuThoDTO duLieuThoDTO = lstDuLieuThoDTO[i];
                    this.Invoke((MethodInvoker)delegate
                    {
                        /*this.DGV_KetQua.Rows.Add(
                            duLieuThoDTO.TenDichVu,
                            duLieuThoDTO.TenDiaDiem,
                            duLieuThoDTO.SoNha + " " + duLieuThoDTO.TenDuong + ", P. " + duLieuThoDTO.TenPhuong,
                            duLieuThoDTO.ChuThich,
                            duLieuThoDTO.TenDuong,
                            duLieuThoDTO.TenPhuong,
                            duLieuThoDTO.KinhDo.ToString(),
                            duLieuThoDTO.ViDo.ToString()
                            );*/

                       // this.DGV_KetQua.Rows[i].Tag = duLieuThoDTO;
                    });
                }
            }
        }

        public void PrepareDetailedResult(List<DuLieuThoDTO> lstDuLieuThoDTO)
        {
            if (null != this._lstDuLieuDTO)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    this.ffpResult.Controls.Clear();
                });

                for (int i = 0; i < lstDuLieuThoDTO.Count; i++)
                {
                    DuLieuThoDTO duLieuThoDTO = lstDuLieuThoDTO[i];
                    MyUC myUC = new MyUC(
                        duLieuThoDTO.TenDiaDiem,
                        duLieuThoDTO.SoNha + " " + duLieuThoDTO.TenDuong + ", P. " + duLieuThoDTO.TenPhuong + ", Q. " + duLieuThoDTO.TenQuanHuyen + ", " + duLieuThoDTO.TenTinhThanh,
                        duLieuThoDTO.TenDichVu,
                        duLieuThoDTO.ChuThich,duLieuThoDTO.KinhDo,duLieuThoDTO.ViDo);
                    this.Invoke((MethodInvoker)delegate
                    {
                        ffpResult.Controls.Add(myUC);
                    });
                }
            }
        }

        private void CB_DichVu_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DichVuDTO dichVuDTO = (DichVuDTO)this.CB_DichVu.SelectedItem;
                if (@"Tất cả dịch vụ" == dichVuDTO.TenDichVu)
                {
                    this.LB_Status.Text = @"Tất cả";
                }
                else
                {
                    this.LB_Status.Text = dichVuDTO.TenDichVu;
                }
            }
            catch (System.Exception ex)
            {
                Utilities.WriteLog(ex.Message);
            }
        }

        //private void DGV_KetQua_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        //{
        //    if (e.RowIndex >= 0 && e.RowIndex < this.DGV_KetQua.RowCount)
        //    {
        //        DataGridViewRow row = this.DGV_KetQua.Rows[e.RowIndex];
        //        DuLieuThoDTO duLieuThoDTO = (DuLieuThoDTO)row.Tag;
        //        if (null != duLieuThoDTO)
        //        {
        //            gridCustomToolTip.SetInformation(
        //                duLieuThoDTO.TenDiaDiem,
        //                duLieuThoDTO.SoNha + " " + duLieuThoDTO.TenDuong + ", P. " + duLieuThoDTO.TenPhuong + ", Q. " + duLieuThoDTO.TenQuanHuyen + ", " + duLieuThoDTO.TenTinhThanh,
        //                duLieuThoDTO.TenDichVu,
        //                duLieuThoDTO.KinhDo.ToString(),
        //                duLieuThoDTO.ViDo.ToString(),
        //                duLieuThoDTO.ChuThich);
        //            this.gridToolTip.Height = this.gridCustomToolTip.lblTenDiaDiem.Height + this.gridCustomToolTip.lblThongTin.Height + 30;
        //            if (!gridToolTip.Visible)
        //            {
        //                Rectangle rect = DGV_KetQua.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
        //                gridToolTip.Show(DGV_KetQua, rect);
        //            }
        //        }
        //    }
        //}

        private void DGV_KetQua_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            this.gridToolTip.Close();
        }

        //private void PB_GoogleMap_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        if (null != this.DGV_KetQua.SelectedRows && this.DGV_KetQua.SelectedRows.Count > 0)
        //        {
        //            DataGridViewRow row = this.DGV_KetQua.SelectedRows[0];
        //            DuLieuThoDTO duLieuThoDTO = (DuLieuThoDTO)row.Tag;
        //            if (null != duLieuThoDTO)
        //            {
        //                Form_ChiTietDichVu frm = new Form_ChiTietDichVu();
        //                frm.SetInformation(
        //                    duLieuThoDTO.TenDiaDiem,
        //                    duLieuThoDTO.SoNha + " " + duLieuThoDTO.TenDuong + ", P. " + duLieuThoDTO.TenPhuong + ", Q. " + duLieuThoDTO.TenQuanHuyen + ", " + duLieuThoDTO.TenTinhThanh,
        //                    duLieuThoDTO.TenDichVu,
        //                    duLieuThoDTO.KinhDo.ToString(),
        //                    duLieuThoDTO.ViDo.ToString(),
        //                    duLieuThoDTO.ChuThich);
        //                frm.ShowDialog();
        //            }
        //        }
        //        else
        //        {
        //            MessageBox.Show("Vui lòng chọn địa điểm.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //        }
        //    }
        //    catch (System.Exception ex)
        //    {
        //        Utilities.WriteLog(ex.Message);
        //    }
        //}

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            //panel_TimNangCao.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.staticbutton6_hover;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            //panel_TimNangCao.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.staticbutton6;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void fireView_Event(object sender, EventArgs e)
        {
            string viewProviderName = this.viewProviders.ProviderName;
            if ("Hiện đại" == viewProviderName)
            {
                //DGV_KetQua.Visible = false;
                ffpResult.Visible = true;
            }
            else
            {
                //DGV_KetQua.Visible = true;
                ffpResult.Visible = false;
            }

            this.viewPopup.Close();
            //this.PB_Arrow.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.arrow_down;
            this._arrowDown = true;
        }

        private void DGV_KetQua_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((e.RowIndex % 2 == 0))
            {
                e.CellStyle.BackColor = Color.CadetBlue;
            }
        }

        private void ShowPagingControl(int start, int end)
        {
            this.Invoke((MethodInvoker)delegate
            {
                if (0 == start)
                {
                    this.LB_Truoc.Visible = false;
                }
                else
                {
                    this.LB_Truoc.Visible = true;
                }

                if (end >= this._lstDuLieuDTO.Count)
                {
                    this.LB_Sau.Visible = false;
                }
                else
                {
                    this.LB_Sau.Visible = true;
                }
            });
        }

        private void ShowPage(int pageIdx)
        {
            int start = pageIdx * ROWS_PER_PAGE;
            int end = (pageIdx + 1) * ROWS_PER_PAGE;

            this.ShowPagingControl(start, end);
            List<DuLieuThoDTO> lstDuLieuThoDTO = this._se.Reference2(this._lstDuLieuDTO, start, end);
            this.PrepareListResult(lstDuLieuThoDTO);
            this.PrepareDetailedResult(lstDuLieuThoDTO);
        }

        private void LB_Truoc_Click(object sender, EventArgs e)
        {            
            this._pageIdx--;
            this.ShowPage(this._pageIdx);
        }

        private void LB_Sau_Click(object sender, EventArgs e)
        {
            this._pageIdx++;
            this.ShowPage(this._pageIdx);          
        }

        //private void TB_InputAdvance_KeyDown(object sender, KeyEventArgs e)
        //{
        //    string temp = TB_InputAdvance.Text + (char)e.KeyCode;
        //    if (temp != "")
        //    {
        //        listBox1.Items.Clear();
        //        String txt = TB_InputAdvance.Text + (char)e.KeyCode;
        //        List<TuKhoaDiaDiemDTO> lstTuKhoaDiaDiem = _ac.AutoCompleteTuKhoaDiaDiem(txt);
        //        int nLstTenDiaDiem = lstTuKhoaDiaDiem.Count;
        //        for (int i = 0; i < nLstTenDiaDiem; i++)
        //        {
        //            listBox1.Items.Add(lstTuKhoaDiaDiem[i].TuKhoaTenDiaDiem);
        //        }
        //        if (listBox1.Items.Count != 0)
        //        {
        //            listBox1.Visible = true;
        //        }
        //        else
        //        {
        //            listBox1.Visible = false;
        //        }
        //    }
        //    else
        //    {
        //        listBox1.Visible = false;
        //    }
        //}

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //TB_InputAdvance.Text = listBox1.SelectedItem.ToString();
                //listBox1.Visible = false;
            }
            catch (Exception ex)
            {

            }
        }

        private void TB_Input_KeyDown(object sender, KeyEventArgs e)
        {
            DichVuDTO dichVuDTO = (DichVuDTO)this.CB_DichVu.SelectedItem;
            if (dichVuDTO.TenDichVu == "Tất cả dịch vụ")
            {
                string temp = TB_Input.Text + (char)e.KeyCode;
                if (temp != "")
                {

                    listBox2.Items.Clear();
                    String txt = TB_Input.Text + (char)e.KeyCode;
                    //địa điểm
                    List<TuKhoaDiaDiemDTO> lstTuKhoaDiaDiem = _ac.AutoCompleteTuKhoaDiaDiem(txt);
                    int nLstTenDiaDiem = lstTuKhoaDiaDiem.Count;
                    for (int i = 0; i < nLstTenDiaDiem; i++)
                    {
                        listBox2.Items.Add(lstTuKhoaDiaDiem[i].TuKhoaTenDiaDiem);
                    }
                    //Đường
                    List<TuKhoaDuongDTO> lstTuKhoaDuong = _ac.AutoCompleteTuKhoaDuong(txt);
                    int nLstTenDuong = lstTuKhoaDuong.Count;
                    for (int i = 0; i < nLstTenDuong; i++)
                    {
                        listBox2.Items.Add(lstTuKhoaDuong[i].TuKhoaDuong);
                    }
                    if (listBox2.Items.Count != 0)
                    {
                        listBox2.Visible = true;
                    }
                    else
                    {
                        listBox2.Visible = false;
                    }
                }
                else
                {
                    listBox2.Visible = false;
                }
            }
            else
            {
                string temp = TB_Input.Text + (char)e.KeyCode;
                if (temp != "")
                {
                    listBox2.Items.Clear();
                    String txt = TB_Input.Text + (char)e.KeyCode;
                    int maDichVu = dichVuDTO.MaDichVu;
                    //Địa điểm
                    List<TuKhoaDiaDiemDTO> lstTuKhoaDiaDiem = _ac.AutoCompleteTuKhoaDiaDiemTheoDichVu(txt, maDichVu);
                    int nLstTenDiaDiem = lstTuKhoaDiaDiem.Count;
                    for (int i = 0; i < nLstTenDiaDiem; i++)
                    {
                        listBox2.Items.Add(lstTuKhoaDiaDiem[i].TuKhoaTenDiaDiem);
                    }
                    //Đường
                    List<TuKhoaDuongDTO> lstTuKhoaDuong = _ac.AutoCompleteTuKhoaDuongTheoDichVu(txt, maDichVu);
                    int nLstTenDuong = lstTuKhoaDuong.Count;
                    for (int i = 0; i < nLstTenDuong; i++)
                    {
                        listBox2.Items.Add(lstTuKhoaDuong[i].TuKhoaDuong);
                    }
                    if (listBox2.Items.Count != 0)
                    {
                        listBox2.Visible = true;
                    }
                    else
                    {
                        listBox2.Visible = false;
                    }
                }
                else
                {
                    listBox2.Visible = false;
                }
            }
            if (e.KeyCode==Keys.Enter)
            {
                {
                listBox2.Visible = false;
                if (null != this._searchThread)
                {
                    if (this._searchThread.ThreadState == ThreadState.Running)
                    {
                        try
                        {
                            this._searchThread.Abort();
                        }
                        catch (System.Exception ex)
                        {

                        }

                        Thread.Sleep(100);
                    }
                }

                string strInput = this.TB_Input.Text;
                int maDichVu = (int)this.CB_DichVu.SelectedValue;
                if (0 == strInput.Trim().Length && 0 == maDichVu)
                {
                    MessageBox.Show("Vui lòng nhập từ khóa để thực hiện tìm kiếm.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SearchInputObject si = new SearchInputObject(strInput, maDichVu);
                    this._searchThread = new Thread(new ParameterizedThreadStart(this.SearchThread));
                    this._searchThread.Start(si);
                }
            }
            }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                TB_Input.Text = listBox2.SelectedItem.ToString();
                listBox2.Visible = false;
            }
            catch (Exception ex)
            {

            }
        }

        private void TSMI_TimCoBan_Click(object sender, EventArgs e)
        {
            this._firstLoadAdvanceSearch = false;
            this.TSMI_TimNangCao.Checked = false;

            this.PB_Title.Visible = true;
            this.PB_Input.Visible = true;
            this.PB_Combobox.Visible = true;
            //this.PB_Tim.Visible = true;
            this.TB_Input.Visible = true;
            this.CB_DichVu.Visible = true;
            this.Panel_Tim.Visible = true;

            //this.panel_KhungTimNangCao.Visible = false;
        }

       

        private void TSMI_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void TSMI_HuongDan_Click(object sender, EventArgs e)
        {
            Form_Help form = new Form_Help();
            form._parent = this;
            form.ShowDialog();
        }

        private void TB_Input_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.TB_Input.Text.Length > 0)
            {
               // this.TB_Input.SelectAll();
                //this.TB_Input.Text = "";
            }

        }

        private void pictureBox2_MouseHover(object sender, EventArgs e)
        {
            ((PictureBox)sender).BackgroundImage = global::simpleGUI_QLQTPM.Properties.Resources.RoadSort_Hover;
        }

        private void pb_RoadSort_MouseEnter(object sender, EventArgs e)
        {
            ((PictureBox)sender).BackgroundImage = global::simpleGUI_QLQTPM.Properties.Resources.RoadSort_Hover;
        }

        private void pb_RoadSort_MouseLeave(object sender, EventArgs e)
        {
            ((PictureBox)sender).BackgroundImage = global::simpleGUI_QLQTPM.Properties.Resources.RoadSort;
        }

        private void pb_RoadSort_Click(object sender, EventArgs e)
        {
            ((PictureBox)sender).BackgroundImage = global::simpleGUI_QLQTPM.Properties.Resources.RoadSort_Click;
        }

        private void pb_Service_MouseEnter(object sender, EventArgs e)
        {
            ((PictureBox)sender).BackgroundImage = global::simpleGUI_QLQTPM.Properties.Resources.ServiceSort_Hover;
        }

        private void pb_Service_MouseLeave(object sender, EventArgs e)
        {
            ((PictureBox)sender).BackgroundImage = global::simpleGUI_QLQTPM.Properties.Resources.ServiceSort;
        }

        private void pb_Service_Click(object sender, EventArgs e)
        {
            ((PictureBox)sender).BackgroundImage = global::simpleGUI_QLQTPM.Properties.Resources.ServiceSort_Click;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            string _currentKeyword = this.TB_Input.Text.Trim();
            if (_currentKeyword != this._oldKeyword)
            {
                if (_searchAvailable)
                {
                    try
                    {
                        listBox2.Visible = false;
                        if (null != this._searchThread)
                        {
                            if (this._searchThread.ThreadState == ThreadState.Running)
                            {
                                try
                                {
                                    this._searchThread.Abort();
                                }
                                catch (System.Exception ex)
                                {

                                }

                                Thread.Sleep(100);
                            }
                        }

                        string strInput = this.TB_Input.Text;
                        int maDichVu = (int)this.CB_DichVu.SelectedValue;

                        SearchInputObject si = new SearchInputObject(strInput, maDichVu);
                        this._searchThread = new Thread(new ParameterizedThreadStart(this.SearchThread));
                        this._searchThread.Start(si);
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    this._oldKeyword = _currentKeyword;
                }
            }
        }

      

        
    }
}
